#include <stdlib.h>
#include <iostream>
#include <PopupLoggerUI.hpp>
#include "PopupSettings.hpp"

using namespace std;
using namespace Popup;
using namespace PopupUtils;

PopupSettings *PopupSettings::m_instance = 0;

PopupSettings *PopupSettings::instance()
{
  if (m_instance == 0)
  {
    m_instance = new PopupSettings;
    m_instance->serialize(POPUP_READ_FROM_FILE);
  }
  return m_instance;
}

// Re-read from config file
bool PopupSettings::serialize(enum SerializeDirection p_direction)
{
  QString _mode;
  
  if (p_direction == POPUP_SAVE_TO_FILE)
  {
    save();
  }
  else if (p_direction == POPUP_READ_FROM_FILE)
  {
    load();
  }
  return true;
}

void PopupSettings::save()
{
  setValue("Server/name", m_serverName);
  setValue("Server/port", m_serverPort);
  setValue("User/login", m_login);
  if (m_savePassword) {
    setValue("User/password", m_password);
  } else if (this->contains("User/password")) {
    remove("User/password");
  }
  setValue("Misc/tempFolder",m_tempDir);
  setValue("Misc/resourceFolder",m_resourceDir);

  setValue("Misc/browserPath",m_browser);
  setValue("Misc/videoPath",m_videoplayer);
  setValue("Misc/imagePath",m_imageviewer);
  setValue("Misc/statusAtStartup", m_defaultMode);
  setValue("Misc/friends", serializedFriendsLists());

  setValue("ImageViewer/useEmbeddedImageViewer", m_useEmbeddedImageViewer);
  setValue("ImageViewer/automaticImageViewerShow", m_automaticImageViewerShow);
  setValue("ImageViewer/enableTransitionFx", m_enableImageViewerTransitionFx);
  setValue("ImageViewer/enableOpacityFx", m_enableImageViewerOpacityFx);
  setValue("ImageViewer/enableZoomFx", m_enableImageViewerZoomFx);
  setValue("ImageViewer/imageViewerPosition", popupPositionToString(m_imageViewerPosition));
  setValue("ImageViewer/imageViewerTransition", popupTransitionToString(m_imageViewerTransition));

  setValue("Custom/buttons", serializedButtonsList());

  setValue("Appearance/mainDialogStyleSheet", m_mainDialogStyleSheet);
  setValue("Appearance/messageDialogStyleSheet", m_messageDialogStyleSheet);
  setValue("Appearance/framelessMsgDlg", m_framelessMessageDialog);
  setValue("Appearance/promptDialog", m_useFakePrompt);
  setValue("Appearance/customFontEnabled", m_customFontEnabled);
  setValue("Appearance/customFontName", m_customFontName);
  setValue("Appearance/customFontColor", m_customFontColor);
  setValue("Appearance/customFontSize", m_customFontSize);
  setValue("Appearance/customFontIsBold", m_customFontIsBold);
  setValue("Appearance/customFontIsItalic", m_customFontIsItalic);
  setValue("Systray/leftClick", popupSystrayActionToString(m_lClickAction));
  setValue("Systray/rightClick", popupSystrayActionToString(m_rClickAction));
  setValue("Systray/middleClick", popupSystrayActionToString(m_mClickAction));
  setValue("Systray/leftDoubleClick", popupSystrayActionToString(m_ldClickAction));

  setValue("UpdateManager/enabled", m_updateEnabled);
  setValue("UpdateManager/URL", m_updateUrl);
  setValue("UpdateManager/delay", m_updateCheckDelay);
  setValue("UpdateManager/proxy", m_updateProxy);
  setValue("UpdateManager/enableProxy", m_updateProxyEnabled);

  setValue("Notifications/fuck", m_enableFuckNotification);
  setValue("Notifications/like", m_enableLikeNotification);
  setValue("Notifications/suck", m_enableSuckNotification);
  setValue("Notifications/poetry", m_enablePoetryNotification);
  setValue("Notifications/transfers", m_enableTransfersDialogBox);
}
void PopupSettings::load()
{
  // Connection & user settings
  m_serverName   = value("Server/name","194.151.42.55").toString();
  m_serverPort   = value("Server/port","2001").toInt();
  m_login        = value("User/login", "").toString();
  m_savePassword = this->contains("User/password");
  m_password     = value("User/password", "").toString();
  m_defaultMode = value("Misc/statusAtStartup","default").toString();
  // Paths
#ifdef _LINUX
  m_tempDir     = value("Misc/tempFolder","/tmp/popup/temp").toString();
  m_resourceDir = value("Misc/resourceFolder","/tmp/popup/res").toString();
  m_browser     = value("Misc/browserPath","/usr/bin/firefox").toString();
  m_videoplayer = value("Misc/videoPath","/usr/bin/vlc").toString();
  m_imageviewer = value("Misc/imagePath","/usr/bin/eog").toString();
#elif defined _WINDOWS
  m_tempDir     = value("Misc/tempFolder","C:\\\\popup\\temp").toString();
  m_resourceDir = value("Misc/resourceFolder","C:\\\\popup\\res").toString();
  m_browser     = value("Misc/browserPath","iexplore.exe").toString();
  m_videoplayer = value("Misc/videoPath","explorer.exe").toString();
  m_imageviewer = value("Misc/imagePath","explorer.exe").toString();
#endif

  // Friends lists
  if (contains("Misc/friends")) {
    deserializeFriendsLists(value("Misc/friends","").toStringList());
  }

  // Image viewer
  m_useEmbeddedImageViewer =
      value("ImageViewer/useEmbeddedImageViewer","true").toBool();
  m_automaticImageViewerShow =
      value("ImageViewer/automaticImageViewerShow", "false").toBool();
  m_imageViewerPosition = popupStringToPosition(
      value("ImageViewer/imageViewerPosition", "Center,Center").toString());
  m_imageViewerTransition = popupStringToTransition(
        value("ImageViewer/imageViewerTransition",
              "Appear from Right,No effect").toString());
  m_enableImageViewerTransitionFx =
      value("ImageViewer/enableTransitionFx", "true").toBool();
  m_enableImageViewerOpacityFx =
      value("ImageViewer/enableOpacityFx", "true").toBool();
  m_enableImageViewerZoomFx =
      value("ImageViewer/enableZoomFx", "false").toBool();

  // Buttons lists
  if (contains("Custom/buttons")) {
    deserializeButtonsList(value("Custom/buttons").toStringList());
  }

  // Font
  m_customFontEnabled = value("Appearance/customFontEnabled", "false").toBool();
  m_customFontName = value("Appearance/customFontName", "Arial").toString();
  m_customFontColor= value("Appearance/customFontColor", "#4C4C4C").toString();
  m_customFontSize= value("Appearance/customFontSize", "10").toInt();
  m_customFontIsBold= value("Appearance/customFontIsBold", "false").toBool();
  m_customFontIsItalic = value("Appearance/customFontIsItalic", "false").toBool();
  updateCustomFont();
  // Dialog style
  m_useFakePrompt = value("Appearance/promptDialog","false").toBool();
  m_messageDialogStyleSheet = value("Appearance/messageDialogStyleSheet",
            MESSAGE_DIALOG_DEFAULT_STYLESHEET).toString();
  m_framelessMessageDialog = value("Appearance/framelessMsgDlg", "true").toBool();
  // Actions
  m_lClickAction  = stringToPopupSystrayAction(value("Systray/leftClick", "show").toString());
  m_rClickAction  = stringToPopupSystrayAction(value("Systray/rightClick", "show").toString());
  m_mClickAction  = stringToPopupSystrayAction(value("Systray/middleClick", "switchstatus").toString());
  m_ldClickAction = stringToPopupSystrayAction(value("Systray/leftDoubleClick", "exit").toString());
  // Update Manager
  m_updateEnabled = value("UpdateManager/enabled", "true").toBool();
  m_updateUrl     = value("UpdateManager/URL", "http://popup-maqui.googlecode.com/files").toString();
  m_updateCheckDelay = value("UpdateManager/delay", "24").toInt();
  m_updateProxyEnabled = value("UpdateManager/enableProxy", "true").toBool();
  m_updateProxy = value("UpdateManager/proxy", "194.151.42.55:3128").toString();

  m_enableFuckNotification   = true; // value("Notifications/fuck", "true").toBool();
  m_enableLikeNotification   = value("Notifications/like", "true").toBool();
  m_enableSuckNotification   = value("Notifications/suck", "true").toBool();
  m_enablePoetryNotification = value("Notifications/poetry", "true").toBool();
  m_enableTransfersDialogBox = value("Notifications/transfers", "true").toBool();
}
//==============================================================================
// Getters
//==============================================================================

const QString &PopupSettings::serverName() const
{
  return m_serverName;
}
//----------------------------------------------------------

int PopupSettings::serverPort() const
{
  return m_serverPort;
}
//----------------------------------------------------------

const QString &PopupSettings::login() const
{
  return m_login;
}
//----------------------------------------------------------

const QString &PopupSettings::password() const
{
  return m_password;
}
//----------------------------------------------------------

const QString &PopupSettings::temporaryDirectory() const
{
  return m_tempDir;
}
//----------------------------------------------------------

const QString &PopupSettings::resourceDirectory() const
{
  return m_resourceDir;
}
//----------------------------------------------------------

const QString &PopupSettings::browser() const
{
  return m_browser;
}
//----------------------------------------------------------

const QString &PopupSettings::videoPlayer() const
{
  return m_videoplayer;
}
//----------------------------------------------------------

const QString &PopupSettings::imageViewer() const
{
  return m_imageviewer;
}
//----------------------------------------------------------

bool PopupSettings::useEmbeddedImageViewer() const
{
  return m_useEmbeddedImageViewer;
}
//----------------------------------------------------------

bool PopupSettings::automaticImageViewerShow() const
{
  return m_automaticImageViewerShow;
}
//----------------------------------------------------------

bool PopupSettings::isImageViewerTransitionFxEnabled() const
{
  return m_enableImageViewerTransitionFx;
}
//----------------------------------------------------------

bool PopupSettings::isImageViewerOpacityFxEnabled() const
{
  return m_enableImageViewerOpacityFx;
}
//----------------------------------------------------------

bool PopupSettings::isImageViewerZoomFxEnabled() const
{
  return m_enableImageViewerZoomFx;
}
//----------------------------------------------------------

PopupGui::Position PopupSettings::getImageViewerPosition() const
{
  return m_imageViewerPosition;
}
//----------------------------------------------------------

void PopupSettings::getImageViewerPosition(QString & p_hpos,
                                           QString & p_vpos) const
{
  QStringList _pos = popupPositionToString(m_imageViewerPosition).split(",");
  p_hpos = _pos[0];
  p_vpos = _pos[1];
}
//----------------------------------------------------------

PopupGui::Transition PopupSettings::getImageViewerTransition() const
{
  return m_imageViewerTransition;
}

//----------------------------------------------------------

void PopupSettings::getImageViewerTransition(QString & p_hEffect,
                                             QString & p_vEffect) const
{
  QStringList _fx = popupTransitionToString(m_imageViewerTransition).split(",");
  p_hEffect = _fx[0];
  p_vEffect = _fx[1];
}

//----------------------------------------------------------

bool PopupSettings::useFakePrompt() const
{
  return m_useFakePrompt;
}
//----------------------------------------------------------

UserMode PopupSettings::defaultClientMode() const
{
  if (m_defaultMode == "prompt") {
    return POPUP_MODE_PROMPT;
  } else if (m_defaultMode == "muted") {
    return POPUP_MODE_MUTED;
  } else if (m_defaultMode == "textonly") {
    return POPUP_MODE_TEXTONLY;
  } else if (m_defaultMode == "normal") {
    return POPUP_MODE_DEFAULT;
  } else {
    // ERROR!!
    return POPUP_MODE_DEFAULT;
  }
}
//----------------------------------------------------------

QByteArray PopupSettings::fileDialogState() const
{
  return QByteArray(value("gui/filedialog","").toByteArray());
}
//----------------------------------------------------------

const QString & PopupSettings::messageDialogStyleSheet() const
{
  return m_messageDialogStyleSheet;
}
//----------------------------------------------------------

bool PopupSettings::framelessMessageDialog() const
{
  return m_framelessMessageDialog;
}
//----------------------------------------------------------

enum PopupSettings::PopupSystrayAction PopupSettings::systrayAction(QSystemTrayIcon::ActivationReason p_reason) const
{
  enum PopupSettings::PopupSystrayAction _res = POPUP_NO_ACTION;

  switch (p_reason)
  {
  case QSystemTrayIcon::Trigger:
    _res = m_lClickAction; break;
  case QSystemTrayIcon::MiddleClick:
    _res = m_mClickAction; break;
  case QSystemTrayIcon::Context:
    _res = m_rClickAction; break;
  case QSystemTrayIcon::DoubleClick:
    _res = m_ldClickAction; break;
  default:
    _res = POPUP_NO_ACTION; break;
  };
  return _res;
}
//----------------------------------------------------------

bool PopupSettings::friendsList(const QString & p_name,
                                UserList & p_list) const
{
  map<QString, UserList>::const_iterator _list = m_friendsLists.find(p_name);
  if (_list != m_friendsLists.end()) {
    p_list.insert(_list->second.begin(), _list->second.end());
    UserList::const_iterator _it;
    return true;
  } else {
    return false;
  }
}

QList<QString> PopupSettings::friendsListsNames() const
{
  QList<QString> _res;
  map<QString, UserList>::const_iterator _it;
  for (_it = m_friendsLists.begin(); _it != m_friendsLists.end(); _it++) {
    _res.append(_it->first);
  }
  return _res;
}
//----------------------------------------------------------

const PopupCustomButtonList & PopupSettings::customButtons()
{
  return m_buttonsList;
}
//----------------------------------------------------------

PopupCustomButton *PopupSettings::customButton(const QString & p_name)
{
  PopupCustomButtonList::iterator _it = m_buttonsList.begin();
  while (_it != m_buttonsList.end())
  {
    if ((*_it)->name() == p_name) {
      return *_it;
    }
    _it++;
  }
  return 0;
}
//----------------------------------------------------------

bool PopupSettings::addCustomButton(const QString & p_name,
                                    const QString & p_icon,
                                    const QString & p_media,
                                    const QString & p_text)
{
  bool _added = false;

  PopupCustomButton *_btn = customButton(p_name);
  if (_btn != 0)
  {
    _btn->setup(p_icon, p_media, p_text);
  }
  else
  {
    // Create a new button
    m_buttonsList.push_back(new PopupCustomButton(p_name, p_icon,
                                                  p_media, p_text));
    _added = true;
  }
  return _added;
}

//----------------------------------------------------------

void PopupSettings::deleteCustomButton(const QString & p_name)
{
  PopupCustomButtonList::iterator _it = m_buttonsList.begin();
  while (_it != m_buttonsList.end())
  {
    if ((*_it)->name() == p_name) {
      delete(*_it);
      m_buttonsList.erase(_it);
      return;
    }
    _it++;
  }
}
//----------------------------------------------------------

const QString & PopupSettings::updateUrl() const
{
  return m_updateUrl;
}
//----------------------------------------------------------

int PopupSettings::updateCheckDelay() const
{
  return m_updateCheckDelay;
}
//----------------------------------------------------------

bool PopupSettings::updateEnabled() const
{
  return m_updateEnabled;
}
//----------------------------------------------------------

const QString & PopupSettings::updateProxy() const
{
  return m_updateProxy;
}
//----------------------------------------------------------

bool PopupSettings::updateProxyEnabled() const
{
  return m_updateProxyEnabled;
}

bool PopupSettings::isFuckNotificationEnabled() const
{
  return m_enableFuckNotification;
}

bool PopupSettings::isLikeNotificationEnabled() const
{
  return m_enableLikeNotification;
}
bool PopupSettings::isSuckNotificationEnabled() const
{
  return m_enableSuckNotification;
}
bool PopupSettings::isPoetryNotificationEnabled() const
{
  return m_enablePoetryNotification;
}
bool PopupSettings::isTransfersDialogBoxEnabled() const
{
  return m_enableTransfersDialogBox;
}
bool PopupSettings::isDisplayAllowed(PopupGui::ReceivedMsgType p_type) const
{
  switch (p_type) {
    case PopupGui::POPUP_RECVD_MESSAGE:
    case PopupGui::POPUP_RECVD_CANVASS:
      return true;
    case PopupGui::POPUP_RECVD_CANVASS_VOTE:
      return false;
    case PopupGui::POPUP_RECVD_FUCK_NOTIF:
      return true; //isFuckNotificationEnabled();
    case PopupGui::POPUP_RECVD_SUCK_NOTIF:
      return isSuckNotificationEnabled();
    case PopupGui::POPUP_RECVD_LIKE_NOTIF:
      return isLikeNotificationEnabled();
    case PopupGui::POPUP_RECVD_POETRY_NOTIF:
      return isPoetryNotificationEnabled();
    default:
      return false;
  }
}
void PopupSettings::setFuckNotificationEnabled(bool p_enabled)
{
  m_enableFuckNotification = p_enabled;
}
void PopupSettings::setLikeNotificationEnabled(bool p_enabled)
{
  m_enableLikeNotification = p_enabled;
}
void PopupSettings::setSuckNotificationEnabled(bool p_enabled)
{
  m_enableSuckNotification = p_enabled;
}
void PopupSettings::setPoetryNotificationEnabled(bool p_enabled)
{
  m_enablePoetryNotification = p_enabled;
}
void PopupSettings::setTransfersDialogBoxEnabled(bool p_enabled)
{
  m_enableTransfersDialogBox = p_enabled;
}

//==============================================================================
// Setters
//==============================================================================

void PopupSettings::setServerName(const QString & p_host)
{
  m_serverName = p_host;
}
//----------------------------------------------------------

void PopupSettings::setServerPort(int p_port)
{
  m_serverPort = p_port;
}
//----------------------------------------------------------

void PopupSettings::setLogin(const QString & p_login)
{
  m_login = p_login;
}
//----------------------------------------------------------

void PopupSettings::setPassword(const QString & p_password)
{
  m_password = p_password;
}
//----------------------------------------------------------

void PopupSettings::setTemporaryDirectory(const QString & p_dir)
{
  m_tempDir = p_dir;
}
//----------------------------------------------------------

void PopupSettings::setResourceDirectory(const QString & p_dir)
{
  m_resourceDir = p_dir;
}
//----------------------------------------------------------

void PopupSettings::setBrowserPath(const QString & p_path)
{
  m_browser = p_path;
}
//----------------------------------------------------------

void PopupSettings::setVideoPlayerPath(const QString & p_path)
{
  m_videoplayer = p_path;
}
//----------------------------------------------------------

void PopupSettings::setImageViewerPath(const QString & p_path)
{
  m_imageviewer = p_path;
}
//----------------------------------------------------------

void PopupSettings::setUseEmbeddedImageViewer(bool p_enabled)
{
  m_useEmbeddedImageViewer = p_enabled;
}
//----------------------------------------------------------

void PopupSettings::setAutomaticImageViewerShow(bool p_enabled)
{
  m_automaticImageViewerShow = p_enabled;
}
//----------------------------------------------------------

void PopupSettings::setEnableImageViewerOpacityFx(bool p_enabled)
{
  m_enableImageViewerOpacityFx = p_enabled;
}
//----------------------------------------------------------

void PopupSettings::setEnableImageViewerZoomFx(bool p_enabled)
{
  m_enableImageViewerZoomFx = p_enabled;
}
//----------------------------------------------------------
void PopupSettings::setEnableImageViewerTransitionFx(bool p_enabled)
{
  m_enableImageViewerTransitionFx = p_enabled;
}
//----------------------------------------------------------

void PopupSettings::setImageViewerTransitionFx(QString p_xEffect,
                                               QString p_yEffect)
{
  m_imageViewerTransition =
      popupStringToTransition(p_xEffect + "," + p_yEffect);
}
//----------------------------------------------------------

void PopupSettings::setImageViewerPosition(QString posX, QString posY)
{
  m_imageViewerPosition = popupStringToPosition(posX + "," + posY);
}
//----------------------------------------------------------

void PopupSettings::setUseFakePrompt(bool p_enabled)
{
  m_useFakePrompt = p_enabled;
}
//----------------------------------------------------------

void PopupSettings::setDefaultClientMode(UserMode p_mode)
{
  switch (p_mode)
  {
  case POPUP_MODE_PROMPT:
    m_defaultMode = "prompt";
    break;
  case POPUP_MODE_MUTED:
    m_defaultMode = "muted";
    break;
  case POPUP_MODE_TEXTONLY:
    m_defaultMode = "textonly";
    break;
  case POPUP_MODE_DEFAULT:
  default:
    m_defaultMode = "normal";
    break;
  }
}
//----------------------------------------------------------

void PopupSettings::setDefaultClientMode(const QString & p_mode)
{
  m_defaultMode = p_mode;
}
//----------------------------------------------------------

void PopupSettings::setFileDialogState(const QByteArray & p_state)
{
  setValue("gui/filedialog", p_state);
}
//----------------------------------------------------------

void PopupSettings::setMessageDialogStyleSheet(const QString & p_style)
{
  m_messageDialogStyleSheet = p_style;
}
//----------------------------------------------------------

void PopupSettings::setFramelessMessageDialog(bool p_frameless)
{
  m_framelessMessageDialog = p_frameless;
}
//----------------------------------------------------------

void PopupSettings::setSystrayAction(QSystemTrayIcon::ActivationReason p_reason,
				     enum PopupSystrayAction p_action)
{
  switch (p_reason)
  {
  case QSystemTrayIcon::Trigger:
    m_lClickAction = p_action; break;
  case QSystemTrayIcon::Context:
    m_rClickAction = p_action; break;
  case QSystemTrayIcon::MiddleClick:
    m_mClickAction = p_action; break;
  case QSystemTrayIcon::DoubleClick:
    m_ldClickAction = p_action; break;
  default:
    break;
  };
}
//---------------------------------------------------------

void PopupSettings::setSavePassword(bool p_save)
{
  m_savePassword = p_save;
}
//----------------------------------------------------------

void PopupSettings::setFriendsList(const QString & p_name,
                                   const UserList & p_list)
{
  m_friendsLists[p_name] = p_list;
}
//----------------------------------------------------------

void PopupSettings::setUpdateUrl(const QString & p_url)
{
  m_updateUrl = p_url;
}
//----------------------------------------------------------

void PopupSettings::setUpdateCheckDelay(int p_delay)
{
  m_updateCheckDelay = p_delay;
}
//----------------------------------------------------------

void PopupSettings::setUpdateEnabled(bool p_enabled)
{
  m_updateEnabled = p_enabled;
}
//----------------------------------------------------------

void PopupSettings::setUpdateProxy(const QString & p_proxy)
{
  m_updateProxy = p_proxy;
}
//----------------------------------------------------------

void PopupSettings::setUpdateProxyEnabled(bool p_enabled)
{
  m_updateProxyEnabled = p_enabled;
}
//----------------------------------------------------------

void PopupSettings::setCustomFontEnabled(bool p_enabled)
{
  m_customFontEnabled = p_enabled;
}
//----------------------------------------------------------

void PopupSettings::setCustomFontName(const QString & p_name)
{
  if (p_name != customFontName())
  {
    m_customFontName = p_name;
    updateCustomFont();
  }
}
//----------------------------------------------------------

void PopupSettings::setCustomFontSize(int p_size)
{
  if (p_size != customFontSize())
  {
    m_customFontSize = p_size;
    updateCustomFont();
  }
}
//----------------------------------------------------------

void PopupSettings::setCustomFontColor(const QString & p_color)
{
  if (p_color.compare(customFontColor()) != 0)
  {
    m_customFontColor = p_color;
    updateCustomFont();
  }
}
//----------------------------------------------------------

void PopupSettings::setCustomFontIsBold(bool p_isBold)
{
  m_customFontIsBold = p_isBold;
  updateCustomFont();
}

//----------------------------------------------------------

void PopupSettings::setCustomFontIsItalic(bool p_isItalic)
{
  m_customFontIsItalic = p_isItalic;
  updateCustomFont();
}

//----------------------------------------------------------

void PopupSettings::updateCustomFont()
{
  // Set prefix
  m_customFontPrefix = QString(
          "<font style='color:%1;font-family:%2;font-size:%3px;%4%5'>").arg(
          m_customFontColor, m_customFontName,
          QString::number(m_customFontSize),
          (m_customFontIsItalic? "font-style:italic;" : ""),
          (m_customFontIsBold?   "font-weight:bold;"  : ""));

  // Set suffix
  m_customFontSuffix = "</font>";
}
//----------------------------------------------------------

bool PopupSettings::customFontEnabled() const
{
  return m_customFontEnabled;
}
//----------------------------------------------------------

const QString & PopupSettings::customFontName() const
{
  return m_customFontName;
}
//----------------------------------------------------------

int PopupSettings::customFontSize() const
{
  return m_customFontSize;
}
//----------------------------------------------------------

const QString & PopupSettings::customFontColor() const
{
  return m_customFontColor;
}
//----------------------------------------------------------

const QString & PopupSettings::customFontPrefix() const
{
  return m_customFontPrefix;
}
//----------------------------------------------------------

const QString & PopupSettings::customFontSuffix() const
{
  return m_customFontSuffix;
}
//----------------------------------------------------------

bool PopupSettings::customFontIsBold() const
{
  return m_customFontIsBold;
}
//----------------------------------------------------------

bool PopupSettings::customFontIsItalic() const
{
  return m_customFontIsItalic;
}

//==============================================================================
// Conversions
//==============================================================================

QString PopupSettings::popupSystrayActionToString(enum PopupSystrayAction p_action)
{
  switch (p_action)
  {
  case POPUP_SHOW_WINDOW:
    return "show";
  case POPUP_EXIT:
    return "exit";
  case POPUP_SWITCH_CLIENT_MODE:
    return "switchstatus";
  case POPUP_NO_ACTION:
  default:
    return "none";
  }
}
//----------------------------------------------------------

PopupSettings::PopupSystrayAction PopupSettings::stringToPopupSystrayAction(const QString & p_string)
{
  if (p_string == "show") {
    return POPUP_SHOW_WINDOW;
  } else if (p_string == "exit") {
    return POPUP_EXIT; 
  } else if (p_string == "switchstatus") {
    return POPUP_SWITCH_CLIENT_MODE;
  } else if (p_string == "none") {
    return POPUP_NO_ACTION;
  } else {
    return POPUP_NO_ACTION;
  }
}


QString PopupSettings::popupPositionToString(PopupGui::Position p_pos)
{
  QString _string;

  // Set X
  if (p_pos.testFlag(PopupGui::POPUP_LEFT)) {
    _string += "Left";
  } else if (p_pos.testFlag(PopupGui::POPUP_HCENTER)) {
    _string += "Center";
  } else if (p_pos.testFlag(PopupGui::POPUP_RIGHT)) {
    _string += "Right";
  } else {
    _string += "Unknown";
  }

  // Separator
  _string += ",";

  // Set Y
  if (p_pos.testFlag(PopupGui::POPUP_TOP)) {
    _string += "Top";
  } else if (p_pos.testFlag(PopupGui::POPUP_VCENTER)) {
    _string += "Center";
  } else if (p_pos.testFlag(PopupGui::POPUP_BOTTOM)) {
    _string += "Bottom";
  } else {
    _string += "Unknown";
  }

  return _string;
}

QString PopupSettings::popupTransitionToString(PopupGui::Transition p_flags)
{
  QString _string;

  // Set horizontal transition
  if (p_flags.testFlag(PopupGui::POPUP_TRANSITION_FROM_RIGHT)) {
    _string += "Appear from Left";
  } else if (p_flags.testFlag(PopupGui::POPUP_TRANSITION_FROM_LEFT)) {
    _string += "Appear from Right";
  } else {
    _string += "No effect";
  }

  // Separator
  _string += ",";

  // Set Y
  if (p_flags.testFlag(PopupGui::POPUP_TRANSITION_FROM_BOTTOM)) {
    _string += "Appear from Bottom";
  } else if (p_flags.testFlag(PopupGui::POPUP_TRANSITION_FROM_TOP)) {
    _string += "Appear from Top";
  } else {
    _string += "No effect";
  }

  return _string;
}

PopupGui::Transition PopupSettings::popupStringToTransition(const QString & p_string)
{
  PopupGui::Transition _flags = 0;
  bool _rc = true;
  QStringList s = p_string.split(",");

  if (s.length() != 2)
  {
    _rc = false;
  }
  else
  {
    if (s[0].compare("Appear from Left", Qt::CaseInsensitive) == 0) {
      _flags |= PopupGui::POPUP_TRANSITION_FROM_RIGHT;
    }
    else if (s[0].compare("Appear from Right", Qt::CaseInsensitive) == 0) {
      _flags |= PopupGui::POPUP_TRANSITION_FROM_LEFT;
    }
    else if (s[0].compare("No effect", Qt::CaseInsensitive) != 0) {
      _rc = false;
    }

    if (s[1].compare("Appear from Top", Qt::CaseInsensitive) == 0) {
      _flags |= PopupGui::POPUP_TRANSITION_FROM_TOP;
    }
    else if (s[1].compare("Appear from Bottom", Qt::CaseInsensitive) == 0) {
      _flags |= PopupGui::POPUP_TRANSITION_FROM_BOTTOM;
    }
    else if (s[0].compare("No effect", Qt::CaseInsensitive) != 0) {
      _rc = false;
    }
  }

  if (!_rc)
  {
    error("Cannot convert <%s> into PopupGui::Transition",
                p_string.toStdString().c_str());
  }

  return _flags;
}

PopupGui::Position PopupSettings::popupStringToPosition(const QString & p_string)
{
  int _pos = 0;
  bool _rc = true;
  QStringList s = p_string.split(",");

  if (s.length() != 2)
  {
    _rc = false;
  }
  else
  {
    if (s[0].compare("Left", Qt::CaseInsensitive) == 0) {
      _pos |= PopupGui::POPUP_LEFT;
    }
    else if (s[0].compare("Center", Qt::CaseInsensitive) == 0) {
      _pos |= PopupGui::POPUP_HCENTER;
    }
    else if (s[0].compare("Right", Qt::CaseInsensitive) == 0) {
      _pos |= PopupGui::POPUP_RIGHT;
    }
    else {
      _rc = false;
    }

    if (s[1].compare("Top", Qt::CaseInsensitive) == 0) {
      _pos |= PopupGui::POPUP_TOP;
    }
    else if (s[1].compare("Center", Qt::CaseInsensitive) == 0) {
      _pos |= PopupGui::POPUP_VCENTER;
    }
    else if (s[1].compare("Bottom", Qt::CaseInsensitive) == 0) {
      _pos |= PopupGui::POPUP_BOTTOM;
    } else {
      _rc = false;
    }
  }

  if (!_rc)
  {
    error("Cannot convert <%s> into PopupGui::Position",
          p_string.toStdString().c_str());
  }

  return (PopupGui::Position) _pos;
}

//==============================================================================
// Friends lists serialization utilities
//==============================================================================

QStringList PopupSettings::serializedFriendsLists() const
{
  QStringList _lists;
  map<QString, UserList>::const_iterator _it;
  for (_it = m_friendsLists.begin(); _it != m_friendsLists.end(); _it++) {
    QString _list = _it->first;
    _list += QString("@#@");
    UserList::const_iterator _itid;
    for (_itid = _it->second.begin(); _itid != _it->second.end(); _itid++) {
      _list += QString::number((ulong) *_itid, 16) + "|";
    }
    _lists.append(_list);
  }
  return _lists;
}

QStringList PopupSettings::serializedButtonsList() const
{
  QStringList _lists;
  const_collection_foreach(m_buttonsList, PopupCustomButtonList, _it)
  {
    _lists.append((*_it)->toString());
  }
  return _lists;
}

void PopupSettings::deserializeFriendsLists(const QStringList & p_lists)
{
  for (int _i = 0; _i < p_lists.size(); _i++) {
    QString _list = p_lists[_i];
    QStringList _nameAndIds = _list.split("@#@");
    QString _name = _nameAndIds.at(0);
    QStringList _ids  = _nameAndIds.at(1).split("|");
    UserList _friends;
    for (int _j = 0; _j < _ids.size(); _j++) {
      bool _ok = false;
      UserID _id = (UserID) _ids[_j].toULong(&_ok, 16);
      if (_ok) {
        _friends.insert(_id);
      }
    }
    m_friendsLists[_name] = _friends;
  }
}

void PopupSettings::deserializeButtonsList(const QStringList & p_buttons)
{
  for (int _i = 0; _i < p_buttons.size(); _i++) {
    PopupCustomButton *_button = PopupCustomButton::fromString(p_buttons[_i]);
    if (_button != 0)
    {
      m_buttonsList.push_back(_button);
    }
  }
}

//==============================================================================
// Default style sheets
//==============================================================================

const QString PopupSettings::MESSAGE_DIALOG_DEFAULT_STYLESHEET =
  "#mainFrame {\n"
  "  border: 3px solid gray;\n"
  "  border-radius: 20px;\n"
  "  background: qlineargradient(spread:pad, x1:0.538, y1:1, x2:0.543, y2:0, stop:0 rgba(200, 200, 200, 255), stop:1 rgba(255, 255, 255, 255));\n"
  "}\n"
  "#m_newThreadTab {\n"
  " background: qlineargradient(spread:pad, x1:0.538, y1:1, x2:0.543, y2:0, stop:0 rgba(200, 200, 200, 255), stop:1 rgba(255, 255, 255, 255));\n"
  "}\n"
  "#m_avatarButton {\n"
  "  border: 1px solid gray;\n"
  "  border-radius: 5px;\n"
  "  background:white;\n"
  "}\n"
  "#m_modeButton {\n"
  "  border: 1px solid gray;\n"
  "  border-radius: 5px;\n"
  "  background:white;\n"
  "}\n"
  "#m_topFrame {\n"
  "border-radius: 15px;\n"
  "  background:qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.682, fx:0.5, fy:0.506, stop:0 rgb(174, 216, 255), stop:1 rgba(255, 255, 255, 255))\n"
  "}\n"
  "#m_likeFrame {\n"
  "  border: 1px solid gray;\n"
  "  border-radius: 5px;\n"
  "  background:qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.682, fx:0.5, fy:0.506, stop:0 rgb(202, 202, 202), stop:1 rgba(255, 255, 255, 255))\n"
  "}\n"
  "#m_dontlikeFrame {\n"
  "  border: 1px solid gray;\n"
  "  border-radius: 5px;\n"
  "  background:qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.682, fx:0.5, fy:0.506, stop:0 rgb(202, 202, 202), stop:1 rgba(255, 255, 255, 255))\n"
  "}\n"
  "#m_poetryFrame {\n"
  "  border: 1px solid gray;\n"
  "  border-radius: 5px;\n"
  "  background:qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.682, fx:0.5, fy:0.506, stop:0 rgb(202, 202, 202), stop:1 rgba(255, 255, 255, 255))\n"
  "}\n"
  "#m_fuckFrame {\n"
  "  border: 1px solid gray;\n"
  "  border-radius: 5px;\n"
  "  background:qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.682, fx:0.5, fy:0.506, stop:0 rgb(202, 202, 202), stop:1 rgba(255, 255, 255, 255))\n"
  "}\n"
  "#m_userNicknameEdit {\n"
  "  border: 1px solid gray;\n"
  "  border-radius: 5px;\n"
  "  background:white;\n"
  "}\n";
